#include "stdafx.h"
#include "tile_actor.hpp"
#include <boost/bind.hpp>
#include <iostream>

namespace FndGame2D {
  const std::string quad_actor::SET_IMAGE("set_image") ;
  const std::string quad_actor::SET_RECT("set_rect") ;

  quad_actor::quad_actor(const std::string& name, int draw_level,
			 const mover::position_type& p,
			 float angle) :
    mover(name, draw_level,p, angle),
    m_sprite(new sf::Sprite) {
    renderer = boost::bind(&quad_actor::quad_defalut_render, this, _1) ;

    updater2.connect(sigc::mem_fun(this, &quad_actor::update_pos_and_rotation)) ;
    message_handler.add_handle(SET_IMAGE, boost::bind(
						      &quad_actor::handle_set_image_message,
						      this,
						      _1)) ;
    message_handler.add_handle(SET_RECT, boost::bind(
						     &quad_actor::handle_set_quad_message,
						     this,
						     _1)) ;
  }

  void quad_actor::quad_defalut_render(sf::RenderTarget& target) {
    
    target.Draw(*m_sprite) ;
  }

  bool quad_actor::handle_set_image_message(const normal_thinker_message_type& msg) {
   
    try {
      const sf::Image* img = boost::any_cast<sf::Image*>(msg.param) ;
      if(img)      
	set_image(*img) ;
      else {
	pantheios::log_DEBUG("handle_set_image_message image not valid ", msg.receiver, "\n") ;
	return false ;
      }
      return true ;
    } catch(const boost::bad_any_cast&) {
      pantheios::log_DEBUG("handle_set_image_message image is not right", msg.receiver, "\n") ;
      return false ;
    }
  }
  bool quad_actor::handle_set_quad_message(const normal_thinker_message_type& msg) {
    try {
      sf::IntRect rect = boost::any_cast<sf::IntRect>(msg.param) ;
      set_rect(rect) ;
      return true ;
    } catch(const boost::bad_any_cast&) {
      return false ;
    }
  }

  sf::FloatRect quad_actor::bounding_box() const {

    return sf::FloatRect(x(),
		       y(),
		       x() + width(),
		       y() + height()) ;
  }
}
